using System;
using System.Collections;
using System.Reflection;
using DataBroker.Dao;

namespace NorthwindDAO
{
	public class Products : Entity
	{
		private Int32 _ProductID;
		private String _ProductName;
		private Int32 _SupplierID;
		private Int32 _CategoryID;
		private String _QuantityPerUnit;
		private Decimal _UnitPrice;
		private Int16 _UnitsInStock;
		private Int16 _UnitsOnOrder;
		private Int16 _ReorderLevel;
		private Boolean _Discontinued;
	
		///Default constructor
		public Products ()
		{
		
		}

        /// <summary>
        /// Constructor for joins. 
		/// Available CHILD tables to join with:
		///    - Order details ( Order details_fk_order_details_products )
		/// Available PARENT tables to join with:
		///    - Categories
		///    - Suppliers
        /// </summary>
        public Products(bool Flat, params Entity[] entities)
            : this()
		{
			foreach(Entity entity in entities)
			{
				string[] entityParts = entity.GetType().ToString().Split('.');
				string entityName = entityParts[entityParts.Length - 1];
				PropertyInfo pf = GetType().GetProperty((entityName));
				if (pf != null)
				{
					pf.SetValue(this,new EntityCollection(entity), null);
				}
                entityName = entityName + "_"; ;
                pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, entity, null);
                }
			}
			SetFlat(Flat);
		}
        public Products(params Entity[] entities)
            : this(true, entities)
		{
		}
	
		[IsNotNullable]
		[IsPK]
		[IsIdentity]
		[SequenceName("Products")]
		public Int32 Productid
		{
			get
			{
				return _ProductID;
			}
		}
		[IsNotNullable]
		[MaxLength(40)]
		public String Productname
		{
			get
			{
				return _ProductName;
			}
			set
			{
				_ProductName = value;
			}
		}
		public Int32 Supplierid
		{
			get
			{
				return _SupplierID;
			}
			set
			{
				_SupplierID = value;
			}
		}
		public Int32 Categoryid
		{
			get
			{
				return _CategoryID;
			}
			set
			{
				_CategoryID = value;
			}
		}
		[MaxLength(20)]
		public String Quantityperunit
		{
			get
			{
				return _QuantityPerUnit;
			}
			set
			{
				_QuantityPerUnit = value;
			}
		}
		[DefaultValue("(0)")]
		public Decimal Unitprice
		{
			get
			{
				return _UnitPrice;
			}
			set
			{
				_UnitPrice = value;
			}
		}
		[DefaultValue("(0)")]
		public Int16 Unitsinstock
		{
			get
			{
				return _UnitsInStock;
			}
			set
			{
				_UnitsInStock = value;
			}
		}
		[DefaultValue("(0)")]
		public Int16 Unitsonorder
		{
			get
			{
				return _UnitsOnOrder;
			}
			set
			{
				_UnitsOnOrder = value;
			}
		}
		[DefaultValue("(0)")]
		public Int16 Reorderlevel
		{
			get
			{
				return _ReorderLevel;
			}
			set
			{
				_ReorderLevel = value;
			}
		}
		[IsNotNullable]
		[DefaultValue("(0)")]
		public Boolean Discontinued
		{
			get
			{
				return _Discontinued;
			}
			set
			{
				_Discontinued = value;
			}
		}
	
        #region Order details 
        private EntityCollection _Order_details;
        [NotInTable]
        [TypeContained(typeof(Order_details),"Order details")]
        public EntityCollection Order_details
        {
            get
            {
                return _Order_details;
            }

            set
            {
                _Order_details = value;
            }
        }
        private Order_details _Order_details_;
        [NotInTable]
        public Order_details Order_details_
        {
            get
            {
                return _Order_details_;
            }

            set
            {
                _Order_details_ = value;
            }
        }
        #endregion
        #region Categories 
        private EntityCollection _Categories;
        [NotInTable]
        [TypeContained(typeof(Categories),"Categories")]
        public EntityCollection Categories
        {
            get
            {
                return _Categories;
            }

            set
            {
                _Categories = value;
            }
        }
        private Categories _Categories_;
        [NotInTable]
        public Categories Categories_
        {
            get
            {
                return _Categories_;
            }

            set
            {
                _Categories_ = value;
            }
        }
        #endregion
        #region Suppliers 
        private EntityCollection _Suppliers;
        [NotInTable]
        [TypeContained(typeof(Suppliers),"Suppliers")]
        public EntityCollection Suppliers
        {
            get
            {
                return _Suppliers;
            }

            set
            {
                _Suppliers = value;
            }
        }
        private Suppliers _Suppliers_;
        [NotInTable]
        public Suppliers Suppliers_
        {
            get
            {
                return _Suppliers_;
            }

            set
            {
                _Suppliers_ = value;
            }
        }
        #endregion
        public override DBJoin GetJoin(string tableName)
        {
			if (string.Compare(tableName, "Order details", true) == 0)
			{
				ArrayList left = new ArrayList();
				ArrayList right = new ArrayList();
				left.Add(tableName + ".Productid");
				right.Add(GetType().Name + ".Productid");
				return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
			}
	        if (string.Compare(tableName, "Categories", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
				left.Add(tableName + ".Categoryid");
				right.Add(GetType().Name + ".Categoryid");
				return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
			}
	        if (string.Compare(tableName, "Suppliers", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
				left.Add(tableName + ".Supplierid");
				right.Add(GetType().Name + ".Supplierid");
				return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
			}
			return null;
		}
	}
}


